/*
 * @(#)MenuResourceManager.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.resources;

import org.eoti.swing.DefaultMenu;
import org.eoti.swing.DefaultAction;
import org.eoti.swing.text.Mnemonic;

import javax.swing.*;
import java.util.concurrent.*;
import java.awt.event.*;

public class MenuResourceManager
extends GUIResourceManager
{
	protected static final String RES_MENU_NAME="name";
	protected static final String RES_MENU_ITEMS="items";

	protected static ConcurrentHashMap<String, DefaultMenu> menus;
	protected GUIResourceManager parentManager;

	public MenuResourceManager(GUIResourceManager parentManager, String bundleName, String resourceKey, JMenuBar menuBar, MouseListener mouseListener)
	{
		super(bundleName, parentManager.getLocale());
		if(menus == null)
			menus = new ConcurrentHashMap<String, DefaultMenu>();

		String[] keys = getStrings(resourceKey);
		for(String key : keys)
		{
			if( (key == null) || (key.length() == 0))
			{
				menuBar.add(Box.createHorizontalGlue());
				continue;
			}

			menuBar.add(getMenu(key, mouseListener));
		}
	}

	public MenuResourceManager(GUIResourceManager parentManager, String bundleName, String resourceKey, DefaultMenu menu, MouseListener mouseListener)
	{
		super(bundleName, parentManager.getLocale());
		if(menus == null)
			menus = new ConcurrentHashMap<String, DefaultMenu>();

		String[] keys = getStrings(resourceKey);
		for(String key : keys)
		{
			if( (key == null) || (key.length() == 0))
			{
				menu.add(Box.createHorizontalGlue());
				continue;
			}

			menu.add(getMenu(key, mouseListener));
		}
	}

	public static DefaultMenu getLoadedMenu(String prefix)
	{
		if(menus == null) return null;
		return menus.get(prefix);
	}

	/**
     * Constructs a new DefaultMenu from the following keys:
	 * [prefix].name=&File...
     * the name has the ampersand(&amp;) before the letter it wishes to use as the mnemonic
	 * [prefix].items=action.open|action.close||action.exit
	 * where an empty item specifies a separator/spacer
	 *
	 * @param prefix to the keys in the resource
	 * @return DefaultMenu constructed from the resource
	 * @param mouseListener to listen for mouse events
	 */
	public DefaultMenu getMenu(String prefix, MouseListener mouseListener)
	{
		DefaultMenu menu = getLoadedMenu(prefix);
		if(menu != null) return menu;

		menu = new DefaultMenu();
		String name = getString(RES_FMT, prefix, RES_MENU_NAME);
		if(!isDefined(name)) return null;
		menu.setMnemonic(new Mnemonic(name));
		String[] items = getStrings(RES_FMT, prefix, RES_MENU_ITEMS);
		for(String item : items)
		{
			// we'll see if it is a separator
			if(!isDefined(item))
			{
				menu.addSeparator();
				continue;
			}

			// then we'll assume that it is a DefaultAction
			DefaultAction itemAction = ActionResourceManager.getLoadedAction(item);
			if(itemAction != null)
			{
				JMenuItem mi = menu.add(itemAction);
				if(mouseListener != null)
					mi.addMouseListener(mouseListener);

				continue;
			}

			// Then let's assume it is a DefaultMenu
			DefaultMenu itemMenu = getMenu(item, mouseListener);
			if(itemMenu != null)
			{
				menu.add(itemMenu);
				continue;
			}

			// Then we'll assume it is... what?
		}

		menus.put(prefix, menu);
		return menu;
	}
}
